/*
 * Copyright (C) 2010 Peter Schönhofen
 *
 * This file is part of the Akkad Compiler Framework.
 *
 * The Akkad Compiler Framework is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package hu.anaxo.akkad.vm

/**
 * Instruction label
 */

class Label {

  // address of label

  private var address: Int = -1

  /**
   * Set label value
   *
   * @param address address
   */

  def resolve(address: Int) =
    if (this.address == -1)
      this.address = address
    else
      throw Error("Label already resolved")

  /**
   * Get label value
   *
   * @return label value
   */

  def get(): Int =
    if (address != -1)
      address
    else
      throw Error("Label not resolved")
}

/**
 * Generic instruction class
 */

abstract class Instruction

/**
 * Label definition
 */

case class Define(label: Label) extends Instruction

/**
 * Instructions
 */

case class CN(value: Int) extends Instruction
case class GV(level: Int, address: Int) extends Instruction
case class SV(level: Int, address: Int) extends Instruction
case class JP(label: Label) extends Instruction
case class CL(label: Label) extends Instruction
case class JT(label: Label) extends Instruction
case class JF(label: Label) extends Instruction
case class EN(level: Int) extends Instruction
case class RT(level: Int) extends Instruction
case class NG() extends Instruction
case class AD() extends Instruction
case class SB() extends Instruction
case class ML() extends Instruction
case class DV() extends Instruction
case class OD() extends Instruction
case class EQ() extends Instruction
case class LT() extends Instruction
case class GT() extends Instruction
case class NE() extends Instruction
case class LE() extends Instruction
case class GE() extends Instruction
case class PR() extends Instruction
